import {
    getUser,
    userUpdate
} from '../../api/myzone'
import Toast from 'tdesign-miniprogram/toast/index';
Page({
    data: {
        oneHeight: 0,
        oneTop: 0,
        headerHeight: 0,
        userInfo: {}
    },
    onLoad() {
        let menuRect = wx.getMenuButtonBoundingClientRect();
        let deviceWidth = wx.getSystemInfoSync().windowWidth;
        let rpx = (750 / deviceWidth) * Number(menuRect.height + menuRect.top);
        let headerHeight = Math.floor(rpx);
        this.setData({
            oneHeight: menuRect.height,
            oneTop: menuRect.top,
            headerHeight: headerHeight + 256 + 170,
        })
        this.getMyzone();
    },
    async getMyzone() {
        const res = await getUser();
        this.setData({
            userInfo: res
        })
    },
    onShareAppMessage() {
        return {};
    },
    jumpToAddressList() {
        wx.navigateTo({
            url: '/pages/usercenter/address/list/index'
        })
    },

    jumpToQuan() {
        wx.navigateTo({
            url: '/pages/coupon/coupon-list/index'
        })
    },
    jumpToQiYe() {
        wx.navigateTo({
            url: '/pages/qiyerenzheng/qiyerenzheng'
        })
    },
    jumpToSheZhi() {
        wx.navigateTo({
            url: '/pages/mine/set/index'
        })
    },
    jumpToOrderList(e) {
        const {
            status
        } = e.currentTarget.dataset;
        let query = ''
        if (status) {
            query = '?status=' + status
        }
        wx.navigateTo({
            url: '/pages/order/order-list/index' + query
        })
    },
    jumpToInfo() {
        wx.navigateTo({
            url: '/pages/usercenter/person-info/index'
        })
    },
    jumpToVipNo() {
        wx.navigateTo({
            url: '/pages/huiyuanka_yikaitong/huiyuanka_yikaitong'
        })
    },
    onClickCell() {
        let that = this
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album', 'camera'],
            success: async (res) => {
                const {
                    path,
                    size
                } = res.tempFiles[0];
                if (size <= 10485760) {
                    wx.uploadFile({
                        url: 'https://sc.lvlifang01.com/app-api/infra/file/upload', //仅为示例，非真实的接口地址
                        filePath: path,
                        name: 'file',
                        header: {
                            'Content-Type': 'multipart/form-data',
                            Authorization: wx.getStorageSync('Authorization') || '',
                            'tenant-id': wx.getStorageSync('tenantId'),
                        },
                        formData: {
                            file: path
                        },
                        async success(res) {
                            let formdata = JSON.parse(res.data)
                            let params = {
                                avatar: formdata.data
                            }
                            const ret = await userUpdate(params);
                            that.setData({
                                'userInfo.avatar': formdata.data
                            })
                        },
                        fail(error) {
                            let msg = error.data.msg
                            Toast({
                                context: this,
                                selector: '#t-toast',
                                message: msg,
                                theme: 'success',
                            });
                        }
                    })
                } else {
                    Toast({
                        context: this,
                        selector: '#t-toast',
                        message: `图片大小超出限制，请重新上传`,
                        theme: 'success',
                    });
                }
            },
            fail: (err) => {
                Toast({
                    context: this,
                    selector: '#t-toast',
                    message: `上传失败`,
                    theme: 'success',
                });
            }
        });
    }
});